package com.cyt.work.wb.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.work.wb.dao.WbServicerDao;
import com.cyt.work.wb.model.po.WbServicer;
import com.cyt.work.wb.model.vo.WbServicerVo;
import com.cyt.work.wb.service.WbServicerService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 维保模块-服务商表Service层实现类
 *
 * @author shengQiang Yu
 * @date 2019-11-28 9:28:43
 */
@Service("wbServicerService")
public class WbServicerServiceImpl extends BaseServiceImpl<WbServicer> implements WbServicerService {

    @Resource
    private WbServicerDao wbServicerDao;

    @Autowired
    public void setBaseDao(WbServicerDao wbServicerDao) {
        this.baseDao = wbServicerDao;
    }

    /**
     * 查询对象集合
     *
     * @param wbServicerVo 对象
     * @return List 对象集合
     * @author shengQiang Yu
     * @date 2019-11-28 9:28:43
     */
    @Override
    public List<WbServicer> listExample(WbServicerVo wbServicerVo) {
        Example example = new Example(WbServicer.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbServicerVo.getName())) {
            criteria.andLike("name", "%" + wbServicerVo.getName() + "%");
        }
        if (StringUtils.isNotBlank(wbServicerVo.getType())) {
            criteria.andEqualTo("type", wbServicerVo.getType());
        }
        if (StringUtils.isNotBlank(wbServicerVo.getApplyId())) {
            criteria.andEqualTo("applyId", wbServicerVo.getApplyId());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return wbServicerDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param wbServicerVo 接受参数对象
     * @param pageNum      分页数
     * @param pageSize     每页显示条数
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:28:43
     */
    @Override
    public ReturnResult page(WbServicerVo wbServicerVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WbServicer> list = listExample(wbServicerVo);
        PageInfo<WbServicer> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wbServicerVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:28:43
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WbServicerVo wbServicerVo, SysUser sysUser) {
        if (StringUtils.isBlank(wbServicerVo.getId())) {
            WbServicer wbServicer = new WbServicer(sysUser);
            wbServicer.setApplyId(wbServicerVo.getApplyId());
            wbServicer.setName(wbServicerVo.getName());
            wbServicer.setPrincipal(wbServicerVo.getPrincipal());
            wbServicer.setMobile(wbServicerVo.getMobile());
            wbServicer.setPhone(wbServicerVo.getPhone());
            wbServicer.setType(wbServicerVo.getType());
            wbServicer.setAddress(wbServicerVo.getAddress());
            wbServicer.setDescription(wbServicerVo.getDescription());
            wbServicer.setUserId(wbServicerVo.getUserId());
            wbServicer.setRemarks(wbServicerVo.getRemarks());
            wbServicer.setSort(Integer.parseInt(wbServicerVo.getSort()));
            wbServicerDao.insert(wbServicer);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WbServicer wbServicer = wbServicerDao.selectByPrimaryKey(wbServicerVo.getId());
            wbServicer.setApplyId(wbServicerVo.getApplyId());
            wbServicer.setName(wbServicerVo.getName());
            wbServicer.setPrincipal(wbServicerVo.getPrincipal());
            wbServicer.setMobile(wbServicerVo.getMobile());
            wbServicer.setPhone(wbServicerVo.getPhone());
            wbServicer.setType(wbServicerVo.getType());
            wbServicer.setAddress(wbServicerVo.getAddress());
            wbServicer.setDescription(wbServicerVo.getDescription());
            wbServicer.setUserId(wbServicerVo.getUserId());
            wbServicer.setRemarks(wbServicerVo.getRemarks());
            wbServicer.setSort(Integer.parseInt(wbServicerVo.getSort()));
            wbServicer.setUpdateTime(new Date());
            wbServicer.setUpdateUser(sysUser.getId());
            wbServicerDao.updateByPrimaryKey(wbServicer);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wbServicerVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:28:43
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WbServicerVo wbServicerVo, SysUser sysUser) {
        //要修改的字段
        WbServicer wbServicer = new WbServicer();
        wbServicer.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wbServicer.setUpdateUser(sysUser.getId());
        wbServicer.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WbServicer.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbServicerVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbServicerVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        wbServicerDao.updateByExampleSelective(wbServicer, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wbServicerVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:28:43
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WbServicerVo wbServicerVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WbServicer.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbServicerVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbServicerVo.getId().split(",")));
        }
        wbServicerDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 获取服务商树（）
     *
     * @param wbServicerVo 参数接受对象
     * @return ReturnResult
     * @author dingxu
     * @date 2019-8-15 17:15
     */
    @Override
    public ReturnResult dTreeList(WbServicerVo wbServicerVo) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<WbServicer> wbServicerList = this.listExample(wbServicerVo);
        for (WbServicer wbServicer : wbServicerList) {
            Map<String, Object> resultMap = new LinkedHashMap<>();
            resultMap.put("id", wbServicer.getId());
            resultMap.put("title", wbServicer.getName());
            resultMap.put("parentId", "0");//没有父级 全部置为0
            Map<String, String> map = new LinkedHashMap<>();
            map.put("type", "0");
            map.put("isChecked", "0");
            List<Map<String, String>> checkArrList = new ArrayList<>();
            checkArrList.add(map);
            resultMap.put("checkArr", checkArrList);
            resultList.add(resultMap);
        }
        return new ReturnResult(resultList);
    }
}